package idv.arthur.xml;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class AddElement {

	public static void main(String[] args) throws IOException, DocumentException {
		final SAXReader reader = new SAXReader();
		final InputStream ins = new FileInputStream("config.xml");
		final Document document = reader.read(ins);
		ins.close();
		Element root = document.getRootElement();
		Iterator<Element> it = root.elementIterator();
		System.out.println("刪除前");
		Element jph= null;
		while (it.hasNext()) {
			Element e = it.next();
			System.out.println(e.getName());	
			if ( "postbuilders".equalsIgnoreCase(e.getName() ) ){
				e.detach();
				jph = root.addElement("postbuilders");
				jph = jph.addElement("jenkins.plugins.http__request.HttpRequest");
				jph.addElement("url").setText("aaaa");
				jph.addElement("httpMode").setText("POST");
				jph.addElement("returnCodeBuildRelevant").setText("true");
				
			}
			
		}
		System.out.println("jph的內容");
		it = jph.elementIterator();
		while (it.hasNext()) {
			Element e = it.next();
			System.out.println(e.getName()+":"+e.getText());
		}
		
		it = root.elementIterator();
		System.out.println("刪除後");
		while (it.hasNext()) {
			Element e = it.next();
			System.out.println(e.getName());
			if ( "postbuilders".equalsIgnoreCase(e.getName() ) ){
				e = (Element) e.elementIterator().next();
				System.out.println("\t"+e.getName());
				e = (Element) e.elementIterator().next();
				System.out.println("\t"+e.getName());
				
			}
		}
	}

}
